const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const path = require('path')
// 应用实例化
const app = express()
// 导入路由模块
const public_router = require('./router/PublicRouter')
const api_router = require('./router/ApiRouter')

/* -----------配置------- */
// 跨域
// app.use(cors())
// json中间件
app.use(bodyParser.json())
// 表单数据
app.use(bodyParser.urlencoded({ extended: false }))

app.use(express.static(path.join(__dirname,'./www/')))
// 注册路由
public_router.forEach(item => {
    // console.log(item);
    app.use('/public', item)
})
api_router.forEach(item => {
    // console.log(item);
    app.use('/api', item)
})

// 注册错误中间件

//逻辑错误中间件
app.use((err, req, res, next) => {
    res.send('错误：' + err);
    next();
})

app.listen('8080', () => console.log("Server is running at http://localhost:8080"))